class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ flag = 1 for i in range(len(digits)-1, -1, -1): if flag == 1: digits[i] += 1 if digits[i] >= 10: digits[i] = 0 else: flag = 0 if flag == 1: digits.insert(0,1) return digits
方法二
1 2 3 4 5 6 7 8 9 10
class Solution: def plusOne(self, digits): for i in range(len(digits)-1,-1,-1): if digits[i]<9: digits[i]=digits[i]+1 return digits else: digits[i]=0 digits.insert(0,1) return digits